package com.qiaofang.service.impl;

import com.qiaofang.common.config.SystemConfig;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.constant.DelFlagEnum;
import com.qiaofang.common.constant.ExceptionEnum;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.response.CommonResponse;
import com.qiaofang.common.utils.StringUtil;
import com.qiaofang.dto.BindCompanyDTO;
import com.qiaofang.entity.*;
import com.qiaofang.entity.enums.PremisesStatusEnum;
import com.qiaofang.entity.enums.SaleStatusEnum;
import com.qiaofang.mapper.ReportMapper;
import com.qiaofang.repository.CompanyRelationRepository;
import com.qiaofang.repository.CompanyRepository;
import com.qiaofang.repository.CustomerPremisesRepository;
import com.qiaofang.repository.PremisesRepository;
import com.qiaofang.service.FHEstateService;
import com.qiaofang.service.OtherCompanyService;
import com.qiaofang.service.PremisesService;
import com.qiaofang.service.V10ApiService;
import com.qiaofang.mapper.PremisesRelationExternalMapper;
import com.qiaofang.repository.*;
import com.qiaofang.service.*;
import com.qiaofang.vo.PremisesDynamicExternalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class V10ApiServiceImpl implements V10ApiService {

    @Autowired
    private OtherCompanyService otherCompanyService;
    @Autowired
    private FHEstateService fhEstateService;
    @Autowired
    private PremisesService premisesService;
    @Autowired
    private CompanyRelationRepository companyRelationRepository;
    @Autowired
    private PremisesRepository premisesRepository;
    @Autowired
    private CompanyRepository companyRepository;
    @Autowired
    private CustomerPremisesRepository customerPremisesRepository;
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private PremisesDynamicExternalRepository dynamicExternalRepository;
    @Autowired
    private PremisesDynamicRepository premisesDynamicRepository;
    @Autowired
    private PremisesRelationRepository premisesRelationRepository;
    @Autowired
    private OtherCompanyRepository otherCompanyRepository;
    @Autowired
    private PremisesRelationExternalRepository premisesRelationExternalRepository;

    /**
     * 指定总代公司推送V10相关商户
     * agentCompnayId: 可为空,不指定时推送所有绑定分销公司
     * premisesIds: 可为空,不指定时推送总代名下所有楼盘
     * @param devCompanyId
     * @param agentCompnayId
     * @param premisesIds
     * @return
     */
    @Override
    public CommonResponse pushPremisesToV10(Long devCompanyId, Long agentCompnayId, List<Long> premisesIds) {
        CommonResponse response = new CommonResponse();
        if (Objects.isNull(devCompanyId)) {
            response.setResponseCode(CommonResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(CommonResponseCode.RC_PARAMETER_ERROR.getResponseMessage() + ": devCompanyId is null");
            return response;
        }
        List<OtherCompany> otherCompanyList = new ArrayList<>();
        if(Objects.nonNull(agentCompnayId)){
            OtherCompany otherCompany = otherCompanyService.getOtherCompanyByChannelCompanyId(agentCompnayId);
            if(Objects.isNull(otherCompany)){
                response.setResponseCode(CommonResponseCode.RC_PARAMETER_ERROR.getResponseCode());
                response.setResponseMessage(CommonResponseCode.RC_PARAMETER_ERROR.getResponseMessage() + ": agentCompnay is null");
                return response;
            }
            otherCompanyList.add(otherCompany);
        }else{
            otherCompanyList = otherCompanyService.selectOtherCompanyListByDevCompany(devCompanyId);
        }

        if (CollectionUtils.isEmpty(otherCompanyList)) {
            response.setResponseCode(CommonResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(CommonResponseCode.RC_PARAMETER_ERROR.getResponseMessage() + ": 无绑定V10分销公司");
            return response;
        }

        List<Premises> premisesList = null;
        Example example = new Example(Premises.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyId", devCompanyId)
                .andEqualTo("premisesStatus", PremisesStatusEnum.type1.getNo())
                .andEqualTo("saleStatus", SaleStatusEnum.type_1.getNo())
                .andEqualTo("delFlag", DelFlagEnum.UNDEL.getNo());
        if(CollectionUtils.isEmpty(premisesIds)){
            premisesList = premisesService.getListPremisesByExample(example);
        }else {
            criteria.andIn("id", premisesIds);
            premisesList = premisesService.getListPremisesByExample(example);
        }
        if(CollectionUtils.isEmpty(premisesList)){
            response.setResponseCode(CommonResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(CommonResponseCode.RC_PARAMETER_ERROR.getResponseMessage() + ": premises is null");
            return response;
        }

        // 推送楼盘到V10
        fhEstateService.pushPremisesToV10(premisesList, otherCompanyList);

        response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
        response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
        return response;
    }

    @Override
    public CommonResponse deletePremisesToV10(Long premisesId) {
        Premises premises = premisesRepository.findById(premisesId)
                .orElseThrow(()->new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));
        CommonResponse response = new CommonResponse();

        /** 查询 外部楼盘关联表 */
        List<PremisesRelationExternal> premisesExternalList = premisesRelationExternalRepository
                .getPremisesRelationExternal(premisesId);
        if (null == premisesExternalList || premisesExternalList.size() == 0) {
            response.setResponseMessage("该楼盘未绑定V10");
            return response;
        }

        // 同步删除V10中的楼盘
        fhEstateService.deletePremisesToV10(premises,premisesExternalList);

        response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
        response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
        return response;
    }

    @Override
    public CommonResponse deleteReportToV10(Long id) {
        CommonResponse response = new CommonResponse();
        CustomerPremises report = customerPremisesRepository.findById(id)
                .orElseThrow(() -> new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));

        OtherCompany otherCompany = otherCompanyService.getOtherCompanyByChannelCompanyId(report.getStaffCompanyId());
        ReportExternal reportExternal = reportMapper.findReportExternal(id);

        if (otherCompany == null || reportExternal == null) {
            response.setResponseMessage("v10中无该报备");
            return response;
        }

        //同步v10报备转为无效报备
        fhEstateService.deleteReportToV10(report, otherCompany, reportExternal);

        response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
        response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
        return response;
    }

    @Override
    public CommonResponse pushDynamicToV10(Long dynamicId) {
        CommonResponse response = new CommonResponse();

        /** 查询动态 */
        PremisesDynamic premisesDynamic = premisesDynamicRepository.findById(dynamicId)
                .orElseThrow(() -> new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));

        /** 查询动态外部关系 */
        List<PremisesDynamicExternal> externalList = dynamicExternalRepository.listDynamicExternal(dynamicId);

        /** 查询 外部楼盘关联表 */
        List<PremisesRelationExternal> premisesExternalList = premisesRelationExternalRepository
                .getPremisesRelationExternal(premisesDynamic.getPremisesId());
        if (null == premisesExternalList || premisesExternalList.size() == 0) {
            response.setResponseMessage("该楼盘未绑定V10");
            response.setResponseCode("-1");
            return response;
        }

        // 推送楼盘动态到V10
        response = fhEstateService.pushDynamicToV10(premisesDynamic,externalList,premisesExternalList);

        return response;
    }
}
